<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/3/10 10:59
 * Remark:
 */

namespace App\Http\Controllers\Contract;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Contract\AddContractPost;
use App\Http\Requests\Contract\ListContractTypeGet;
use App\Service\Contract\ContractService;

class ContractController extends Controller
{
    private ContractService $_contractService;

    public function __construct(){
        $this->_contractService = new ContractService();
    }

    /**
     * 新增合同
     * @param AddContractPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addContract(AddContractPost $request): \Illuminate\Http\JsonResponse
    {
        $contractData = $request->all();
        $res = $this->_contractService->addContract($contractData);
        return ResponseFunc::returnJson(
            'error',
            [
                'risk_element_id' => $res
            ],
            '新增合同风控要素成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );

    }

    /**
     * 修改合同
     * @param int $contractId
     * @return \Illuminate\Http\JsonResponse
     */
    public function editContract(int $contractId): \Illuminate\Http\JsonResponse
    {
        $data = request()->post();
        $data['contract_id'] = $contractId;
        $this->_contractService->editContract($data);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '修改合同成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 删除合同
     * @param int $contractId
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteContract(int $contractId): \Illuminate\Http\JsonResponse
    {
        $this->_contractService->deleteContract($contractId);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '删除合同成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 合同列表
     * @param ListContractTypeGet $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getContractList(ListContractTypeGet $request): \Illuminate\Http\JsonResponse
    {
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractService->getContractList($pageSize,$filters),
            '获取合同列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 合同详情
     * @param int $contractId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getContractInfo(int $contractId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'error',
            $this->_contractService->getContractInfo($contractId),
            '获取合同详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

}